package com.baizhi.controller;
import com.baizhi.dto.VideoDetailDTO;
import com.baizhi.entity.User;
import com.baizhi.entity.Video;
import com.baizhi.service.VideoService;
import com.baizhi.utils.JSONUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 视频 前端控制器
 * </p>
 *
 * @author 2107class
 * @since 2021-12-22
 */
@RestController
@RequestMapping
public class VideoController {


    private static final Logger log = LoggerFactory.getLogger(VideoServiceController.class);

    @Autowired
    private VideoService videoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/recommends")
    public List<Video> selectPage(Integer page,@RequestParam("per_page") Integer perPage){
        System.out.println(page);
        System.out.println(perPage);
        List<Video> videos = videoService.selectPage(page, perPage);
        return videos;
    }

    /**
     * 查询视频详情
     */

    @GetMapping("/videos/{video_id}")
    public VideoDetailDTO video(@PathVariable("video_id") Integer videoId, String token) {
        log.info("当前接收到的videoId: {}", videoId);
        if (ObjectUtils.isEmpty(videoId)) throw new RuntimeException("提示: 视频已被下架!");
        VideoDetailDTO videoDetailDTO = videoService.queryDetailById(videoId, token);
        log.info("查询到的视频详细对象信息: {}", JSONUtils.writeValueAsString(videoDetailDTO));
        return videoDetailDTO;
    }

    /*@PostMapping("user/videos")
    public Video insertVideo(HttpServletRequest request,MultipartFile file, String title, String intro, String categoryId, String token){

//        User user = (User) redisTemplate.opsForValue().get(token);
        User user = (User) request.getAttribute("user");
        Integer id = user.getId();
        System.out.println("user = " + user);
        Video add = videoService.add(file,title,intro,categoryId,id);
        return add;
    }*/
}

